import java.security.MessageDigest;

/**
 * @author by zyl
 * @date 2020/3/28.
 */
public class HashUtils {
    public static String byteArrayToString(byte[] b) {
        StringBuffer resultSb = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            resultSb.append(byteToNumString(b[i]));
        }
        return resultSb.toString();
    }

    private static String byteToNumString(byte b) {
        int _b = b;
        if (_b < 0) {
            _b = 256 + _b;
        }
        return String.valueOf(_b);
    }


    public static String Hash(String origin) {
        String resultString = null;
        try {
            resultString = new String(origin);
            MessageDigest md = MessageDigest.getInstance("MD5");
            resultString = byteArrayToString(md.digest(resultString.getBytes()));
            if(resultString.length()>16)
                resultString=resultString.substring(0, 16);
        } catch (Exception ex) {

        }
        return resultString;
    }
}